package org.boyan.cases;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.boyan.entity.cases.DemandInfo;
import org.boyan.utils.ExcelExportUtils;
import org.boyan.utils.ExcelUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class DemandInfoToExcel {

    public static void exportDemandInfo(File file, DemandInfo demandInfo) throws IOException {
        String fileName = file.getName();
        Workbook workbook = null;
        InputStream in = null;
        in = new FileInputStream(file);
        if (ExcelUtils.judegExcelEdition(fileName)) {
            workbook = new XSSFWorkbook(in);
        } else {
            workbook = new HSSFWorkbook(in);
        }
        Sheet sheet = workbook.getSheetAt(1);
        sheet.getRow(0).getCell(1).setCellValue(demandInfo.getDemandContact());
        sheet.getRow(0).getCell(3).setCellValue(demandInfo.getBussinessDepartment());
        sheet.getRow(1).getCell(1).setCellValue(demandInfo.getOaNo());
        sheet.getRow(1).getCell(3).setCellValue(demandInfo.getExpectedDate());
        sheet.getRow(2).getCell(1).setCellValue(demandInfo.getDemandName());
        sheet.getRow(3).getCell(1).setCellValue(demandInfo.getOnlineNecessary());
        sheet.getRow(3).getCell(3).setCellValue(demandInfo.getUserQueryAction());
        sheet.getRow(4).getCell(1).setCellValue(demandInfo.getDataSourceSystem());
        sheet.getRow(4).getCell(3).setCellValue(demandInfo.getDataSourceSystemAbbr());
        sheet.getRow(5).getCell(1).setCellValue(demandInfo.getDataSourceSystemPrincipal());
        int isOperation = demandInfo.getIsOperation();
        if (isOperation == 1) {
            sheet.getRow(6).getCell(1).setCellValue("是");
            sheet.getRow(6).getCell(3).setCellValue(demandInfo.getExpectedOperationDate());
        } else {
            sheet.getRow(6).getCell(1).setCellValue("否");
        }
        sheet.getRow(7).getCell(1).setCellValue(demandInfo.getInvokeSystem());
        sheet.getRow(7).getCell(3).setCellValue(demandInfo.getInvokeSystemAbbr());
        sheet.getRow(8).getCell(1).setCellValue(demandInfo.getInvokeSystemPrincipal());
        ExcelExportUtils.writeWorkbook(workbook,file);
        workbook.close();
        in.close();
    }
}
